#include "Timer.h"
#include <windows.h>

Timer::Timer()
{
	m_secondsPerCount = 0.0;
	m_deltaTime = -1.0;
	m_baseTime = 0;
	m_pausedTime = 0;
	m_prevTime = 0;
	m_currTime = 0;
	m_stoped = false;

	__int64 countsPerSecond;
	QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSecond);
	m_secondsPerCount = 1.f/countsPerSecond;

}

void Timer::Tick()
{
	if(m_stoped){
		m_deltaTime = 0.0;
		return;
	}

	QueryPerformanceCounter((LARGE_INTEGER*)&m_currTime);
	m_deltaTime = (float)(m_currTime - m_prevTime);
	m_prevTime = m_currTime;
	if(m_deltaTime < 0.0) m_deltaTime = 0.0;

}

float Timer::GetDeltaTime()
{
	return m_deltaTime * m_secondsPerCount;
}

float Timer::GetTime()
{
	return m_currTime * m_secondsPerCount;
}

void Timer::Reset()
{
	QueryPerformanceCounter((LARGE_INTEGER*)&m_currTime);
	m_baseTime = m_currTime;
	m_prevTime = m_currTime;
	m_stopTime = 0;
	m_stoped = false;
}

void Timer::Start()
{
	if(m_stoped == false) return;
	QueryPerformanceCounter((LARGE_INTEGER*)&m_currTime);
	m_pausedTime += (m_currTime - m_stopTime);
	m_prevTime = m_currTime;
	m_stoped = false;
}

void Timer::Stop()
{
	if(m_stoped) return;
	QueryPerformanceCounter((LARGE_INTEGER*)&m_stopTime);
	m_stoped = true;
}
